package com.zmht.beike.web.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.zmht.beike.core.BaseAction;
import com.zmht.beike.po.StudentTwo;
import com.zmht.beike.po.TCognition;
import com.zmht.beike.po.THabit;
import com.zmht.beike.po.THealthmovement;
import com.zmht.beike.po.TLanguage;
import com.zmht.beike.po.TSociality;
import com.zmht.specedu.vo.Teacher;
/**
 * 统计出图Action
 * @author Administrator
 *
 */
public class StaticChartAction extends BaseAction {
	/**
	 * 初始化单个学生信息查询
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward initStaticSinge(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		//获得当前登录老师
		Teacher teacher = (Teacher)request.getSession().getAttribute("current_user_session_key");
		
		List list = this.getStudentBiz().getStudentDAO().findByHQLObj("from StudentTwo");
		
		request.setAttribute("students", list);
		return new ActionForward("/jsp/pingce/static_singe.jsp");
	}
	/**
	 * 统计当学生
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward staticSingeStudent(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String studentId = request.getParameter("studentId");
		StudentTwo student = (StudentTwo)this.getStudentBiz().getStudentDAO().findById(StudentTwo.class, new Integer(studentId));
		
		StringBuffer sb = new StringBuffer();
		sb.append("<chart showPercentageValues='1' baseFontSize='15' yAxisName='测评分数'  caption='"+student.getSname()+" 发展测评结果' showBorder='0' showValues='0' bgColo='DC143C' >");
		sb.append("<set label='健康与动作' value='"+student.getSumHealth()+"' toolText=''/>"); 
		sb.append("<set label='语言能力' value='"+student.getSumLanguage()+"' toolText=''/>");
		sb.append("<set label='认知发展' value='"+student.getSumCognition()+"' toolText=''/>"); 
		sb.append("<set label='社会性能力' value='"+student.getSumSociality()+"' toolText=''/>");
		sb.append("<set label='习惯养成' value='"+student.getSumHabit()+"' toolText=''/>"); 
		sb.append("</chart>");
		
		request.setAttribute("chart", sb.toString());
		
		
		Teacher teacher = (Teacher)request.getSession().getAttribute("current_user_session_key");
		List list = this.getStudentBiz().getStudentDAO().findByHQLObj("from StudentTwo");
		request.setAttribute("studentId", studentId);
		request.setAttribute("students", list);
		return new ActionForward("/jsp/pingce/static_singe.jsp");
	}
	/**
	 * 初始化按时间统计
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */	
	public ActionForward initStaticStep(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		//获得当前登录老师
		Teacher teacher = (Teacher)request.getSession().getAttribute("current_user_session_key");
		List list = this.getStudentBiz().getStudentDAO().findByHQLObj("from StudentTwo");
		
		request.setAttribute("students", list);
		return new ActionForward("/jsp/pingce/static_step.jsp");
	}
	/**
	 * 按时间段统计
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward staticStepStudent(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String studentId = request.getParameter("studentId");
		String startTime = request.getParameter("startTime");
		String endTime = request.getParameter("endTime");
		String type= request.getParameter("type");
		//Teacher teacher = (Teacher)request.getSession().getAttribute("current_user_session_key");
		
		
		StudentTwo student = (StudentTwo)this.getStudentBiz().getStudentDAO().findById(StudentTwo.class, new Integer(studentId));
		//System.out.println("endTime= "+endTime+" startTime="+startTime);
		
		
		StringBuffer sb = new StringBuffer();
		
		StringBuffer conent = new StringBuffer();
		String tital = "";
		if("1".equals(type)){
			List<THealthmovement> list = this.getHealthMovementBiz().findByStudent(studentId,startTime,endTime);
			tital="健康与动作";			
			for(THealthmovement t:list){
				conent.append("<set label='"+t.getCtime()+"' value='"+t.getScore()+"' toolText=''/>"); 
			}
			
		}else if("2".equals(type)){
			
			List<TLanguage> list = this.getLanguageBiz().findByStudent(studentId,startTime,endTime);
			tital="语言能力";
			for(TLanguage t:list){
				conent.append("<set label='"+t.getCtime()+"' value='"+t.getScore()+"' toolText=''/>"); 
			}
			
		}else if("3".equals(type)){
			List<TCognition> list = this.getCognitionBiz().findByStudent(studentId,startTime,endTime);
			tital="认知发展";
			for(TCognition t:list){
				conent.append("<set label='"+t.getCtime()+"' value='"+t.getScore()+"' toolText=''/>"); 
			}
		}else if("4".equals(type)){
			
			List<TSociality> list =this.getSocialityBiz().findByStudent(studentId,startTime,endTime);
			tital="社会性能力";
			for(TSociality t:list){
				conent.append("<set label='"+t.getCtime()+"' value='"+t.getScore()+"' toolText=''/>"); 
			}
			
		}else if("5".equals(type)){
			List<THabit> list = this.getHabitBiz().findByStudent(studentId,startTime,endTime);
			tital="习惯养成";
			for(THabit t:list){
				conent.append("<set label='"+t.getCtime()+"' value='"+t.getScore()+"' toolText=''/>"); 
			}
		}
		
		sb.append("<chart showPercentageValues='1' baseFontSize='15'  caption='"+student.getSname()+" "+tital+" 发展测评结果' showBorder='0' showValues='0' bgColo='DC143C' >");
		sb.append(conent.toString());
		sb.append("</chart>");
		request.setAttribute("chart", sb.toString());
		
		
		
		List list = this.getStudentBiz().getStudentDAO().findByHQLObj("from StudentTwo");
		request.setAttribute("students", list);
		request.setAttribute("startTime", startTime);
		request.setAttribute("endTime", endTime);
		request.setAttribute("type", type);
		request.setAttribute("studentId", studentId);
		return new ActionForward("/jsp/pingce/static_step.jsp");
	}
	/**
	 * 初始化统计所有孩子
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward initStaticAll(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		//获得当前登录老师
		Teacher teacher = (Teacher)request.getSession().getAttribute("current_user_session_key");
		
		String sql = "select avg(t.sum_health),avg(t.sum_language),avg(t.sum_cognition),avg(t.sum_sociality),avg(t.sum_habit) from student t";
		List<Object []> list = this.getStudentBiz().getStudentDAO().findBySql(sql);
		System.out.println("list size="+list.size());
		Object rs [] = list.get(0);
		
		StringBuffer sb = new StringBuffer();
		sb.append("<chart showPercentageValues='1' baseFontSize='15'  caption=' 所有孩子发展测评平均值' showBorder='0' showValues='0' bgColo='DC143C' >");
		sb.append("<set label='健康与动作' value='12' toolText='"+rs[0]+"'/>"); 
		sb.append("<set label='语言能力' value='12' toolText='"+rs[1]+"'/>");
		sb.append("<set label='认知发展' value='12' toolText='"+rs[2]+"'/>"); 
		sb.append("<set label='社会性能力' value='11' toolText='"+rs[3]+"'/>");
		sb.append("<set label='习惯养成' value='22' toolText='"+rs[4]+"'/>"); 
		sb.append("</chart>");
		
		request.setAttribute("chart", sb.toString());
		
		return new ActionForward("/jsp/pingce/static_all.jsp");
	}
}
